<?php
/**
 * @package contenite/samples
 */
class user extends app_model {
  public $_key = 'id';
  public $_fields = array(
    'id' => array(
      'type' => 'INT(11)'),
    'name' => array(
      'type' => 'VARCHAR(128)'),
    'email' => array(
      'title' => 'E-Mail',
      'type' => 'VARCHAR(128)',
      'access' => 'private'),
    'password' => array(
      'title' => 'Passwort',
      'type' => 'VARCHAR(128)',
      'access' => 'private',
      'widget' => 'password',
      'sql' => 'PASSWORD(%s)'),
    'code' => array(
      'type' => 'VARCHAR(64)'),
    'last' => array(
      'type' => 'INT(11)'));
  public $_unique = array('email');
      
  function dir($user_id) {
    static $dirs;
    if (!isset($dirs[$user_id])) {
      $dir = array();
      while ($user_id > 100) {
        $dir[] = sprintf('%02d', $user_id % 100);
        $user_id /= 100;
      }
    }
    return $dirs[$user_id] = join('/', $dir);
  } 
  
  function avatar($email = NULL) {
    if ($email === NULL) {
      if (isset($_SESSION['user'])) {
        $email = $_SESSION['user']->email;
      }
    }
    if (!$email || $_SERVER['HTTP_HOST'] == 'dankbarkeit') {
      return NULL;
    }
    
    $size = 24;
    return 'http://www.gravatar.com/avatar/' 
      . md5(strtolower($email)) . '.jpg'
      . '?s=' . $size
      . '&d=identicon'; 
  }
}
